home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 …ember: Reference Library / Dev.CD Dec 97 RL.toast / What's New / Tool Chest / Testing & Debugging / Virtual User / Examples / External Tool Templates / CPlus Tool Template / Service.cp < prev    next >
Encoding:
Text File  |  1997-10-15  |  7.1 KB  |  233 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        Service.cp
  3.  *
  4.  *    Contains:    xxx put contents here xxx
  5.  *
  6.  *    Written by:    Rick Violet
  7.  *
  8.  *    Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  9.  *
  10.  *    Change History (most recent first):
  11.  *
  12.  *        <5+>    11/19/92    RV        
  13.  *                11/18/92    RV        xxx put comment here xxx
  14.  *
  15.  *    To Do:
  16.  */
  17.  
  18. #ifndef        __Service__
  19. #include        "Service.h"
  20. #endif
  21.  
  22. #ifndef        __Application__
  23. #include        "Application.h"
  24. #endif
  25.  
  26. #ifndef        __RequestDispatcher__
  27. #include        "RequestDispatcher.h"
  28. #endif
  29.  
  30. #ifndef        __PACKAGES__
  31. #include        <Packages.h>
  32. #endif
  33.  
  34. //—————————————————————————————————————————————————————————————————————————————————————
  35. //                                Global Variables
  36. //—————————————————————————————————————————————————————————————————————————————————————
  37. extern    RequestDispatcher*        gTheRequestDispatcher;
  38. extern    ThreadID                gRequestDispatcherThreadID;
  39. extern    Application*            gTheApplication;
  40.  
  41. //—————————————————————————————————————————————————————————————————————————————————————
  42. //    Service::Service    -    constructor.
  43. //—————————————————————————————————————————————————————————————————————————————————————
  44. Service::Service( char* pSrvName )
  45. {            
  46.     if( pSrvName != nil )
  47.     {
  48.         fSrvNameText = new char[ strlen( pSrvName ) + 1 ];
  49.         if( fSrvNameText != nil )
  50.         {
  51.             strcpy( fSrvNameText, pSrvName );
  52.         }
  53.     }
  54.     else
  55.     {
  56.         fSrvNameText = nil;
  57.     }    
  58.     
  59.             /*SBR Hacked this in 10/16/94 */
  60.         //————    Set the custom timeout interval to the default timeout 
  61.     SetTimeOutInterval( this->GetDefaultTimeOutInterval() );
  62. }
  63.  
  64. //—————————————————————————————————————————————————————————————————————————————————————
  65. //    Service::~Service    -    destructor.
  66. //—————————————————————————————————————————————————————————————————————————————————————
  67. Service::~Service()
  68. {
  69.     if( fSrvNameText != nil )
  70.     {
  71.         delete fSrvNameText;
  72.     }
  73. }
  74.  
  75.  
  76.         /*SBR Hacked this in 10/16/94 */
  77. //—————————————————————————————————————————————————————————————————————————————————————
  78. //    ThreadedService::ThreadedService    -    constructor.
  79. //—————————————————————————————————————————————————————————————————————————————————————
  80. ThreadedService::ThreadedService( char* pSrvName ):Service( pSrvName )
  81. {    
  82.  
  83. }
  84.  
  85.         /*SBR Hacked this in 10/16/94 */
  86. //—————————————————————————————————————————————————————————————————————————————————————
  87. //    ThreadedService::~ThreadedService    -    destructor.
  88. //—————————————————————————————————————————————————————————————————————————————————————
  89. ThreadedService::~ThreadedService()
  90. {
  91. }
  92.  
  93.  
  94. //—————————————————————————————————————————————————————————————————————————————————————
  95. //    Service::CanDoService    -    return true if we can handle the Service indicated.
  96. //—————————————————————————————————————————————————————————————————————————————————————
  97. Boolean
  98. Service::CanDoService( char* pServiceName )
  99. {    
  100.     short    tResult;
  101.     
  102.     if( fSrvNameText != nil )    
  103.     {
  104.         tResult = relstring( pServiceName, fSrvNameText, false, true );
  105.         if( tResult == 0 )
  106.         {
  107.             return true;
  108.         }
  109.     }
  110.     return false;
  111. }
  112.  
  113. //—————————————————————————————————————————————————————————————————————————————————————
  114. //    Service::ProcessRequest    -    implement the Request from V.U.
  115. //
  116. //        This method is called by the Dispatcher to implement the requested service.
  117. //        Override this method within your own subclass to implement the Service.
  118. //        Be sure to call CheckForCancel() frequently to allow for canceling.
  119. //        If CheckForCancel() returns true, then stop processing, and return 
  120. //        immediately. Otherwise continue processing.
  121. //
  122. //—————————————————————————————————————————————————————————————————————————————————————
  123. OSErr
  124. Service::ProcessRequest( Request* pReq )
  125. {
  126.         //————    We do nothing but beep thrice
  127.     SysBeep( 1 );
  128.     SysBeep( 1 );
  129.     SysBeep( 1 );
  130.     
  131.         //————    and return the word 'beeped' to the script
  132.     pReq->SetReturnValue( "beeped" );
  133.     
  134.         //————    no errors encountered
  135.     return noErr;
  136. }
  137.  
  138. //—————————————————————————————————————————————————————————————————————————————————————
  139. //    Service::GetServiceNameText    
  140. //—————————————————————————————————————————————————————————————————————————————————————
  141. ScriptValue*
  142. Service::GetServiceNameText()
  143. {
  144.     VUString*    tTextVal;
  145.     
  146.     tTextVal = new VUString( fSrvNameText );
  147.     
  148.     return tTextVal;
  149. }
  150.  
  151.  
  152.         /*SBR Hacked this in 10/16/94 */
  153. //—————————————————————————————————————————————————————————————————————————————————————
  154. //    Service::CheckForCancel    -    Check for cancel requests and return true
  155. //            if the current request has been canceled
  156. //—————————————————————————————————————————————————————————————————————————————————————
  157. Boolean
  158. Service::CheckForCancel( Request* pReq, Boolean pYield )
  159. {
  160.         //————    if we have received a Cancel message for this request, cancel immediately
  161.     if( pReq->HasBeenCanceled() )
  162.         return true;
  163.     
  164.         //————    If we have not received a Cancel message, give time to other processes
  165.         //————    unless the caller passes false in the pYield parameter.
  166.     if( pYield )
  167.     {
  168.         if( !gTheApplication->IsThreaded() )
  169.         {
  170.                 //————    Threads are not present, just call the Event Manager.
  171.                 //————    Spin the cursor so Humans can see the cancel happening.
  172.                 //————    SpinTheCursor() includes a call to Application::DoEvent()
  173.             gTheApplication->SpinTheCursor();
  174.  
  175.                 //————    reset any Apple event timeout counters which need it
  176.             gTheRequestDispatcher->ResetAllTimeOutCounters();
  177.         }
  178.         else if( pReq->IsThreaded() )
  179.         {
  180.                 //————    Request is threaded, yield the most efficient way. 
  181.             YieldToAnyThread( );
  182.         }
  183.         else
  184.         {
  185.                 //————    Request is non-threaded, but application is threaded. 
  186.                 //————    RequestDispatcher thread executes all non-threaded services. 
  187.                 //————    Always yield to app to keep threaded services from getting 
  188.                 //————    time, while allowing cancel and other events to be handled.
  189.                 //————    The application *always* yields to the RequestDispatcher.
  190.             YieldToThread( kApplicationThreadID );
  191.         }
  192.     }
  193.     
  194.     return false;
  195. }
  196.  
  197.  
  198.         /*SBR Hacked this in 10/16/94 */
  199. //—————————————————————————————————————————————————————————————————————————————————————
  200. //    Service::SetTimeOutInterval    -    Specify a custom timeout interval in seconds.
  201. //                                    You can call this within Service::ProcessRequest().
  202. //                                    Must be > kResetSendThreshold ( see AERequest.cp ).
  203. //—————————————————————————————————————————————————————————————————————————————————————
  204. void
  205. Service::SetTimeOutInterval( long pNewTimeOutInterval ) 
  206.     fTimeOutInterval = pNewTimeOutInterval;
  207. }
  208.  
  209.         /*SBR Hacked this in 10/16/94 */
  210. //—————————————————————————————————————————————————————————————————————————————————————
  211. //    Service::GetTimeOutInterval    -    Read the current time out interval for a service.
  212. //                                    Used by RequestDiapatcher::ResetAllTimeOutCounters().
  213. //—————————————————————————————————————————————————————————————————————————————————————
  214. long
  215. Service::GetTimeOutInterval( ) 
  216.     return fTimeOutInterval;
  217. };
  218.  
  219.         /*SBR Hacked this in 10/16/94 */
  220. //—————————————————————————————————————————————————————————————————————————————————————
  221. //    Service::GetDefaultTimeOutInterval    -    Overload this in your service object to specify
  222. //                                    a custom default timeout interval in seconds ( >10 ).
  223. //—————————————————————————————————————————————————————————————————————————————————————
  224. long
  225. Service::GetDefaultTimeOutInterval( ) 
  226.         // use a different constant in your Service object definition
  227.     return kDefaultTimeOutSeconds;
  228. };
  229.  
  230.